﻿using ERPSystem.Common.Domains.Notices;
using ERPSystem.Common.DTO;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SalesManagementCommon.API.SalesManagementCommons.BillingInvoicing;
using SalesManagementCommon.API.SalesManagementCommons.ReturnNotices;
using SqlSugar;

namespace SalesManagementCommon.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BilleInvoiceController : ControllerBase
    {

        private readonly IMediator _mediator;

        public BilleInvoiceController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 开票通知单显示查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<Result<PageResultDto<BillNoticesDTO>>>GetBilleInvoice([FromQuery]BillingInvoiceQueryCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request, cancellationToken);
            return result;
        }

        /// <summary>
        /// 开票通知单反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<PageResultDto<BillNoticesDTO>>> FanTBilleInvoice([FromQuery] BillingInvoiceBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(request, cancellationToken);
            return result;
        }

        /// <summary>
        /// 开票结算单导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> BillNoticeExport([FromQuery] BillingInvoicingExcelCommand request)
        {
            var result = await _mediator.Send(request);
            return File(result.FileBytes, result.MimeType, result.FileName);
        }

    }
}
